/**
 * @filename:BomController Dec 11, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wonder.app.domain.Bom;
import com.wonder.app.domain.Bomlist;
import com.wonder.app.domain.Synbd;
import com.wonder.app.mapper.BomDao;
import com.wonder.app.mapper.BomlistDao;
import com.wonder.app.mapper.SynbdDao;
import com.wonder.app.service.BomService;
import com.wonder.app.request.BomRequest;
import com.wonder.app.service.BomlistService;
import com.wonder.app.vo.BomVo;
import com.wonder.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： Bom管理API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Dec 11, 2023
 *
 */
@Api(tags = "Bom管理")
@RestController
@RequestMapping("/app/Bom")
public class BomController {
	
	@Autowired
	private BomService bomService;

	@Autowired
	private BomlistService bomlistService;

	@Autowired
	private BomDao bomDao;

	@Autowired
	private BomlistDao bomlistDao;

	@Autowired
	private SynbdDao synbdDao;
	
	@ApiOperation(value = "Bom管理列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody BomRequest bomRequest) {
		Page<Bom> page = new Page<Bom>(bomRequest.getPage(), bomRequest.getLimit());
		IPage<Bom> list = bomService.selectList(bomRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Bom> updateExt(@RequestBody Bom entity){
		if (null!=entity) {
			boolean rsg = bomService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Bom> add(@RequestBody Bom entity){

		if (null!=entity) {
		    
		    String validate=bomService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = bomService.save(entity);
			if (rsg) {
				  return AjaxResult.success("system.insertSuccess");
			  }else {
				  return AjaxResult.failed("system.insertFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取Bom管理对象", notes = "作者：Wonder")
	public AjaxResult<Bom> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		 Bom bom=bomService.getById(id);
		
		if (null!=bom ) {
			 return AjaxResult.success(bom);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Bom> update(@RequestBody Bom entity){
		if (null!=entity) {
		    String validate=bomService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = bomService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("system.updateSuccess");
			  }else {
				  return AjaxResult.failed("system.updateFailed");
			  }
		}else {
			return AjaxResult.failed("system.parameterError");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Bom> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = bomService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.success("system.deleteSuccess");
	    }else {
		   return AjaxResult.failed("system.deleteFailed");
	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   map 对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取Bom管理对象明细", notes = "作者：Wonder")
	public AjaxResult<Bom> detail(@RequestBody Map<String,Object> map){
	    if(map==null) return AjaxResult.failed("system.parameterError");
		Long id=Long.parseLong(map.get("id").toString());		
		Bom bom=bomService.getById(id);
		if (null!=bom ) {	
			 return AjaxResult.success(bom);
		}else {
			 return AjaxResult.failed("system.parameterError");
		}
		
	}

	/**
	 *同步BOM
	 * @throws Exception
	 */
	@PostMapping("/syncbom")
	public void syncBom()throws Exception{
		//获取最后一次同步的数据
		QueryWrapper<Synbd> queryWrappersyn = new QueryWrapper<>();
		queryWrappersyn.eq("Ftype","BOM");
		queryWrappersyn.orderByDesc("Fsyndate");
		queryWrappersyn.last("Limit 1");
		Synbd lastSynbd = synbdDao.selectOne(queryWrappersyn);
		//最后一次同步时间
		Date fsyndate = lastSynbd.getFsyndate();
		String endTime = DateUtils.dateToStr(fsyndate, "yyyy-MM-dd HH:mm:ss");
		//开始同步之前创建一个新的同步数据
		Synbd synbd = new Synbd();
		synbd.setReloadStartTime(new Date());
		synbd.setFtype("BOM");
		try {
			bomService.syncBom(endTime);
			//同步结束修改同步状态
			synbd.setReloadEndTime(new DateTime());
			synbd.setFsyndate(new DateTime());
			synbd.setReloadRes(true);
			synbd.setReloadOrder(false);
			synbd.setReloadMsg("");
			synbdDao.insert(synbd);
		}catch (Exception e){
			synbd.setReloadEndTime(new DateTime());
			synbd.setReloadMsg(e.getMessage());
			synbd.setReloadRes(false);
			synbd.setReloadOrder(false);
			synbdDao.insert(synbd);
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 查看子项明细
	 * @return
	 */
	@PostMapping("/get_childList")
	public AjaxResult<List<Bomlist>> getBomChildList(@RequestBody Map<String,Object> map){

		int bomId = (int) map.get("id");

		Bom bom = bomDao.selectOne(new QueryWrapper<Bom>().eq("id", bomId));
		Long fid = bom.getFID();
		QueryWrapper<Bomlist> wrapper = new QueryWrapper<>();
		wrapper.eq("detailid",fid);
		List<Bomlist> bomChildList = bomlistService.list(wrapper);

		return AjaxResult.success(bomChildList);
	}
}